<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="false">
			<block slot="content">登录</block>
		</cu-custom>
		<view class="login-bg">
			<view class="login-card">
				<view class="login-head">请输入你的账户</view>
				<view class="login-input login-margin-b">
					<input type="text" v-model="phone" placeholder="请输入账号" />
				</view>
				<view class="login-input">
					<input type="password" v-model="password" placeholder="请输入密码(8-16位)" />
				</view>
				<view class="login-function">
					<view class="login-forget" @click="go_forget">忘记密码</view>
					<view class="login-register" @click="go_register">快速注册></view>
				</view>
			</view>
		</view>
		<view class="login-btn">
			<button class="landing" @click="go_login()" type="primary">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				password: ''
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			go_forget() {
				uni.navigateTo({
					url: 'forget'
				})
			},
			go_register() {
				uni.navigateTo({
					url: '../register/checkphone'
				})
			},
			go_login() {
				if (this.phone.length <= 0) {
					uni.showToast({
						title: '请输入账号',
						icon: 'none'
					})
					return
				}
				if (this.password.length <= 0) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}
				uni.request({
					url: this.$constant.BASEURL + this.$constant.USER_LOGIN,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: this.phone,
						password: this.password
					},
					success: (res) => {
						var result = res.data
						console.log(result)
						if (result.code == 0) {
							//保存用户信息
							getApp().globalData.username = result.data.username
							getApp().globalData.departname = result.data.departname
							getApp().globalData.token = result.data.token
							getApp().globalData.orgcode = result.data.orgcode
							// uni.showToast({
							// 	title: '登陆成功',
							// })
							this.saveUserInfo()
							//判断是否绑定了设备
							if(result.data.havedevice||result.data.orgcode.length>3){
								//有设备跳转到主页
								uni.switchTab({
								    url: '../attendance/attendance'
								});
							}else{
								//无设备跳转到设备绑定页
								uni.showModal({
									title: '提示',
									content: '检测到你还未增加设备，为了你的正常使用，请立刻前往添加',
									showCancel: true,
									success: (res) => {
										console.log(res)
										if (res.confirm) {
											uni.navigateTo({
												url:'../device/increase?link=first'
											})
										}else{
											uni.switchTab({
											    url: '../attendance/attendance'
											});
										}
									}
								})
							}
						} else {
							uni.showToast({
								title: result.message,
								icon: 'none'
							})
						}

						//跳转到添加相机页面
						// uni.navigateTo({
						// 	url: '../device/increase'
						// });
					}
				});
			},
			saveUserInfo(){
				uni.setStorage({
				    key: 'userinfo',
				    data: {username:this.phone,password:this.password},
				    success: function () {
				        console.log('success');
				    }
				});
			},
			getUserInfo(){
				var that = this
				uni.getStorage({
					key:'userinfo',
					success:function(res){
						var result = res.data
						that.phone = result.username
						that.password = result.password
					},
					fail() {
						console.log("getStorage error")
					}
				})
			}
		}
	}
</script>

<style>
	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #5599ff, #5599ff);
	}

	.login-btn {
		padding: 10upx 20upx;
		margin-top: 350upx;
	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: left;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: right;
		font-size: 26upx;

	}

	.login-input input {
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;
	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}

	.login-card {
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.12);
		position: relative;
		margin-top: 120upx;
	}

	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#5599ff, #5599ff);
	}
</style>
